package xy.donate.android.helper

import xy.donate.android.constant.Constant
import java.text.SimpleDateFormat
import java.util.*

object TimeUtils {

    val DEFAULT_SDF = SimpleDateFormat(Constant.DATE_FORMAT_DETACH, Locale.getDefault())

    fun formatDateLongToString(
            timeLongVal: Long?,
            descFormat: String?
    ): String? {
        val date = Date(timeLongVal!!)
        val df = SimpleDateFormat(descFormat)
        return df.format(date)
    }


    /**
     * 时间戳转成小时和分钟
     */
    fun convertMinAndSec(time: Int): String? {
        var timeStr: String? = null
        var hour = 0
        var minute = 0
        var second = 0
        if (time <= 0) {
            return "0"
        } else {
            minute = time / 60
            if (minute < 60) {
                second = time % 60
                timeStr =
                        unitFormat(
                                minute
                        ) + ":" + unitFormat(
                                second
                        )
            } else {
                hour = minute / 60
                minute %= 60
                second = time - hour * 3600 - minute * 60
                timeStr =
                        unitFormat(
                                hour
                        ) + ":" + unitFormat(
                                minute
                        ) + ":" + unitFormat(
                                second
                        )
            }
        }
        return timeStr
    }

    fun unitFormat(i: Int): String? {
        var retStr: String? = null
        retStr = if (i in 0..9) "0$i" else "" + i
        return retStr
    }

    /**
     * 获取当前时间
     *
     * 格式为yyyy-MM-dd HH:mm:ss
     *
     * @return 时间字符串
     */
    fun getCurTimeString(): String? {
        return date2String(Date())
    }

    /**
     * 将Date类型转为时间字符串
     *
     * 格式为yyyy-MM-dd HH:mm:ss
     *
     * @param time Date类型时间
     * @return 时间字符串
     */
    fun date2String(time: Date?): String? {
        return date2String(time, DEFAULT_SDF)
    }

    /**
     * 将Date类型转为时间字符串
     *
     * 格式为用户自定义
     *
     * @param time   Date类型时间
     * @param format 时间格式
     * @return 时间字符串
     */
    fun date2String(time: Date?, format: SimpleDateFormat): String? {
        return format.format(time)
    }

}